package www.dianbaozu.com.ui

import androidx.hilt.lifecycle.ViewModelInject
import androidx.lifecycle.viewModelScope
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.target.Target
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.launch
import www.wen.com.common.lib.app.BaseApplication
import www.wen.com.common.lib.base.module.BaseApiViewModel
import www.wen.com.common.lib.data.entities.Result
import www.wen.com.common.lib.data.entities.StartPageEntity
import www.wen.com.common.lib.data.repository.AppDataManager
import www.wen.com.common.lib.extension.glide.GlideApp
import www.wen.com.common.lib.helper.loading.KeyHelper

/**
 * @author WEN
 * @Description:
 * @date 2021/8/23 11:14
 */
class SplashViewModel @ViewModelInject constructor(appDataManager: AppDataManager) :
    BaseApiViewModel(appDataManager) {

    /**
     * 启动页数据缓存
     */
    fun requestStartPage(callBack: (startPageEntity: StartPageEntity) -> Unit) {
        viewModelScope.launch(Dispatchers.Main) {
            flow {
                appDataManager.queryCache(
                    KeyHelper.CACHE_START_KEY,
                    Result.Error(Exception("No DATA")),
                    successBlock = { _, startPageEntity: StartPageEntity ->
                        if (startPageEntity.content.isNotEmpty()) {
                            if(startPageEntity.content.isNotEmpty()){
                                GlideApp.with(BaseApplication.CONTEXT)
                                    .load(startPageEntity.content[0])
                                    .diskCacheStrategy(DiskCacheStrategy.DATA)
                                    .preload(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
                            }
                        }
                        emit(startPageEntity)
                    },
                    errorBlock = { _, error: Result.Error ->
                        throw Exception(error.exception)
                    })
            }
                .flowOn(Dispatchers.IO)
                .catch {
                    GlobalScope.launch {
                        val result = appDataManager.requestStartPage(20, 0)
                        if (result is Result.Success) {
                            appDataManager.insertCache(KeyHelper.CACHE_START_KEY, result.data)
                            if(result.data.content.isNotEmpty()){
                                GlideApp.with(BaseApplication.CONTEXT)
                                    .load(result.data.content[0])
                                    .diskCacheStrategy(DiskCacheStrategy.DATA)
                                    .preload(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
                            }
                        }
                    }
                }.collect {
                    callBack.invoke(it)
                    val result = appDataManager.requestStartPage(20, 0)
                    if (result is Result.Success) {
                        if (result.data.content.isNotEmpty()) {
                            appDataManager.insertCache(KeyHelper.CACHE_START_KEY, result.data)
                        }
                    }
                }
        }
    }

}
